<!--企业资质弹窗-->
<template>
    <Modal
        :value="value"
        width="746"
        :closable="false"
        :loading="loading"
        @input="handleInput"
        class-name="qualifications-modal"
    >
        <div slot="header">
            编辑企业资质信息
            <Icon class="f-fr" type="ios-close" @click="closeModel"/>
        </div>
        <Form class="clearfix" inline :label-width="120" :model="qualificationsForm" ref="qualificationsRef" :rules="rule">
            <FormItem label="企业资质类别" prop="entQualifTypeCode">
                <Select
                    v-model="qualificationsForm.entQualifTypeCode"
                    filterable
                    transfer
                    clearable
                    label-in-value
                    :disabled="editORshow"
                    @on-change="changeCode"
                    placeholder="企业资质类别"
                >
                    <Option
                        v-for="(option, index) in enterpriseList"
                        :key="index"
                        :value="option.dataCode"
                        :label="option.dataName"
                    >{{ option.dataCode + '/' + option.dataName }}</Option>
                </Select>
            </FormItem>
            <FormItem label="企业资质编号" prop="entQualifNo">
                <Input
                    v-model="qualificationsForm.entQualifNo"
                    type="text"
                    :disabled="editORshow"
                    placeholder="企业资质编号"
                ></Input>
            </FormItem>
        </Form>
        <div class="btn-box">
            <Button type="primary" @click="addEnterprise" :disabled="editORshow">新增</Button>
            <Button type="primary" @click="saveEnterprise" :disabled="editORshow">保存</Button>
            <Button type="primary" @click="delEnterprise" :disabled="editORshow">删除</Button>
        </div>
        <div class="table-modal">
            <Table
                @on-selection-change="enterpriseSelect"
                :columns="enterpriseColumns"
                :data="tableDataList"
            >
            </Table>
        </div>
        <Checkbox v-model="isAgree" style="color:red;">
            企业承诺：本单位持有海关要求的合格保证、标签标识及其他证明声明材料，知悉相关材料内容，保证符合法律法规要求，并自存留档。
        </Checkbox>
        <div slot="footer">
            <Button type="default" @click="closeModel">取消</Button>
            <Button type="primary" @click="saveEnterpriseQua">确定</Button>
        </div>
    </Modal>
</template>

<script>
const enterpriseListJK = [
    { dataCode: '000', dataName: '企业产品许可类别' },
    { dataCode: '100', dataName: '通关司类' },
    { dataCode: '101', dataName: '检疫处理单位审批' },
    { dataCode: '200', dataName: '卫生司类' },
    { dataCode: '300', dataName: '动植司类' },
    { dataCode: '303', dataName: '进境水果境外果园/包装厂注册登记' },
    { dataCode: '306', dataName: '进口饲料和饲料添加剂生产企业注册登记' },
    { dataCode: '307', dataName: '进境非食用动物产品生产、加工、存放企业注册登记' },
    { dataCode: '312', dataName: '进境植物繁殖材料隔离检疫圃申请' },
    { dataCode: '317', dataName: '进出境动物指定隔离检疫场使用申请' },
    { dataCode: '319', dataName: '进境栽培介质使用单位注册' },
    { dataCode: '320', dataName: '进境动物遗传物质进口代理及使用单位备案' },
    { dataCode: '321', dataName: '进境动物及动物产品国外生产单位注册' },
    { dataCode: '322', dataName: '饲料进口企业备案' },
    { dataCode: '326', dataName: '进境粮食加工储存单位注册' },
    { dataCode: '327', dataName: '进口粮食境外出口、仓储企业注册登记' },
    { dataCode: '400', dataName: '检验司类' },
    { dataCode: '413', dataName: '进口可用作原料的固体废物国内收货人注册登记' },
    { dataCode: '414', dataName: '进口可用作原料的固体废物国外供货商注册登记' },
    { dataCode: '415', dataName: '进出境集装箱场站登记' },
    { dataCode: '416', dataName: '进口棉花境外供货商登记注册' },
    { dataCode: '418', dataName: '对出口食品包装生产企业和进口食品包装的进口商实行备案' },
    { dataCode: '421', dataName: '进出口商品检验鉴定机构许可' },
    { dataCode: '500', dataName: '食品局类' },
    { dataCode: '508', dataName: '进口食品境外出口商代理商备案' },
    { dataCode: '509', dataName: '进口食品进口商备案' },
    { dataCode: '510', dataName: '进口肉类收货人备案' },
    { dataCode: '511', dataName: '进口肉类存储冷库备案' },
    { dataCode: '513', dataName: '进口水产品存储冷库备案' },
    { dataCode: '515', dataName: '进口化妆品收货人备案' },
    { dataCode: '524', dataName: '进口食品境外生产企业信息记录' },
    { dataCode: '600', dataName: '综合类' },
    { dataCode: '601', dataName: '进口其他证书' },
    { dataCode: '603', dataName: '从事进出境检疫处理业务的人员认定' },
    { dataCode: '700', dataName: '认监委类' }
]
const enterpriseListCK = [
    { dataCode: '000', dataName: '企业产品许可类别' },
    { dataCode: '100', dataName: '通关司类' },
    { dataCode: '101', dataName: '检疫处理单位审批' },
    { dataCode: '102', dataName: '实施绿色通道制度申请' },
    { dataCode: '200', dataName: '卫生司类' },
    { dataCode: '300', dataName: '动植司类' },
    { dataCode: '301', dataName: '出境水果包装厂注册登记' },
    { dataCode: '302', dataName: '出境水果果园注册登记' },
    { dataCode: '304', dataName: '出境水生动物养殖场/包转场检验检疫注册登记' },
    { dataCode: '305', dataName: '出口饲料和饲料添加剂生产、加工、存放企业注册登记' },
    { dataCode: '308', dataName: '出境货物木质包装除害处理标识加施资格申请' },
    { dataCode: '309', dataName: '出境种苗花卉生产经营企业注册登记' },
    { dataCode: '310', dataName: '出境竹木草制品生产企业注册登记' },
    { dataCode: '311', dataName: '出口植物产品生产、加工、存放企业注册登记' },
    { dataCode: '315', dataName: '供港澳陆生动物饲养场、中转场检验检疫注册' },
    { dataCode: '317', dataName: '进出境动物指定隔离检疫场使用申请' },
    { dataCode: '318', dataName: '出境动物及其非食用动物产品生产、加工、存放企业注册登记' },
    { dataCode: '323', dataName: '饲料出口企业备案' },
    { dataCode: '324', dataName: '出境货物木质包装除害处理合格凭证' },
    { dataCode: '329', dataName: '出境粮食生产、加工、存放企业注册登记' },
    { dataCode: '400', dataName: '检验司类' },
    { dataCode: '415', dataName: '进出境集装箱场站登记' },
    { dataCode: '417', dataName: '出口玩具质量许可（注册登记）' },
    { dataCode: '418', dataName: '对出口食品包装生产企业和进口食品包装的进口商实行备案' },
    { dataCode: '419', dataName: '输美日用陶瓷生产厂认证' },
    { dataCode: '421', dataName: '进出口商品检验鉴定机构许可' },
    { dataCode: '500', dataName: '食品局类' },
    { dataCode: '501', dataName: '出口肉类产品养殖场备案' },
    { dataCode: '502', dataName: '出口蛋禽养殖场备案' },
    { dataCode: '503', dataName: '出口蜂产品养蜂基地备案' },
    { dataCode: '504', dataName: '出口食品原料种植场备案' },
    { dataCode: '505', dataName: '供港澳蔬菜生产加工企业备案' },
    { dataCode: '506', dataName: '供港澳蔬菜种植基地备案' },
    { dataCode: '507', dataName: '出口粮谷豆类生产加工企业注册登记' },
    { dataCode: '512', dataName: '出口加工用水产养殖场备案' },
    { dataCode: '514', dataName: '出口化妆品生产企业备案' },
    { dataCode: '520', dataName: '出口食品生产企业境外注册' },
    { dataCode: '602', dataName: '出口其他证书' },
    { dataCode: '603', dataName: '从事进出境检疫处理业务的人员认定' },
    { dataCode: '700', dataName: '认监委类' }
]
export default {
    name: 'QualificationsModal',
    props: {
        value: {
            default: false,
            type: Boolean
        },
        qualifications: {
            default: function () {
                return {
                    originList: [],
                    isAgree: false,
                    editORshow: false
                }
            },
            type: Object
        }
    },
    data () {
        const entQualifNoCheck = (rule, value, callback) => {
            if (value && value.length > 41) {
                callback(new Error('企业资质编号长度不能超过41'))
            } else {
                callback()
            }
        }

        return {
            loading: false,
            editORshow: null,
            enterpriseList: [],
            qualificationsForm: {
                entQualifTypeCode: '',
                entQualifTypeName: '',
                entQualifNo: ''
            },
            enterpriseColumns: [
                {
                    type: 'selection',
                    align: 'center'
                },
                {
                    title: '企业资质类别代码',
                    key: 'entQualifTypeCode',
                    tooltip: true
                },
                {
                    title: '企业资质类别名称',
                    key: 'entQualifTypeName',
                    tooltip: true
                },
                {
                    title: '企业资质编号',
                    key: 'entQualifNo',
                    tooltip: true
                }
            ],
            tableDataList: [],
            selectedDataList: [],
            isAgree: false,
            declaratioMaterialCode: '',
            rule: {
                entQualifNo: [
                    { validator: entQualifNoCheck, trigger: 'blur' }
                ]
            }
        }
    },
    created () {
        let data = this.qualifications
        this.init(data.originList, data.isAgree, data.editORshow, data.type)
    },
    methods: {
        init (originList, isAgree, editORshow, type) {
            this.enterpriseList = /^(j|J)/.test(type) ? enterpriseListJK : enterpriseListCK
            if (originList.length > 0) {
                originList.forEach(item => {
                    item._disabled = editORshow
                })
            }
            this.editORshow = editORshow
            this.tableDataList = originList
            this.isAgree = isAgree
        },

        handleInput (v) {
            this.$emit('input', v)
        },

        closeModel () {
            this.addEnterprise()
            this.handleInput(false)
        },

        changeCode (val) {
            if (val) {
                this.qualificationsForm.entQualifTypeName = val.label
            } else {
                this.qualificationsForm.entQualifTypeName = ''
            }
        },

        // 清空 form 表单
        addEnterprise () {
            this.$refs['qualificationsRef'].resetFields()
        },

        saveEnterprise () {
            if (!this.qualificationsForm.entQualifTypeCode) {
                this.$Message.warning('请选择企业资质类别')
                return false
            }

            if (!this.qualificationsForm.entQualifNo) {
                this.$Message.warning('请输入企业资质编号')
                return false
            }

            this.$refs['qualificationsRef'].validate((valid) => {
                if (!valid) {
                    return false
                }

                let len = this.tableDataList.length
                if (len === 0) {
                    this.tableDataList.push(JSON.parse(JSON.stringify(this.qualificationsForm)))
                } else {
                    let isRepeat = false
                    this.tableDataList.forEach(item => {
                        if (item.entQualifTypeCode === this.qualificationsForm.entQualifTypeCode) {
                            isRepeat = true
                        }
                    })

                    if (isRepeat) {
                        this.$Message.warning('企业资质类别' + '"' + this.qualificationsForm.entQualifTypeName + '"' + '已存在，不能重复添加！')
                        return false
                    } else {
                        this.tableDataList.push(JSON.parse(JSON.stringify(this.qualificationsForm)))
                    }
                }

                this.addEnterprise()
            })
        },

        delEnterprise () {
            if (this.selectedDataList.length === 0) {
                this.$Message.warning('请先勾选数据')
                return false
            }
            let delList = this.selectedDataList
            let tableList = this.tableDataList
            for (let i = 0; i < delList.length; i++) {
                for (let j = 0; j < tableList.length; j++) {
                    if (delList[i].entQualifTypeCode === tableList[j].entQualifTypeCode) {
                        tableList.splice(j, 1)
                    }
                }
            }
        },

        enterpriseSelect (val) {
            this.selectedDataList = val
        },

        saveEnterpriseQua () {
            console.log(this.tableDataList)
            let res = {
                qualificationsList: this.tableDataList,
                isAgree: this.isAgree
            }
            this.$emit('saveEnterpriseQua', res)
            this.closeModel()
        }
    }
}
</script>

<style lang="less">
    .qualifications-modal {
        .btn-box {
            margin-bottom: 10px;
            button:nth-child(2) {
                margin: 0 10px;
            }
        }

        .table-modal {
            margin-bottom: 10px;
        }
    }
</style>
